<template>
  <div class="login">
    <div class="login_box">
      <h3 class="login-title">健身房管理系统</h3>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="" prop="tel">
          <el-input v-model="ruleForm.tel" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="" prop="pwd">
          <el-input v-model="ruleForm.pwd" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;">立即登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { managerLogin } from '@/api/login'
import md5 from 'crypto-md5'
export default {
  name: '',
  components: {},
  data() {
    return {
      ruleForm: {
        tel: 17605415332,
        pwd: '123456'
      },
      rules: {
        tel: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min:6, max: 12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      },
      checked: false
    }
  },
  mounted() {
    if (localStorage.getItem('checked')) {
      this.checked = true
      this.ruleForm.tel = localStorage.getItem('tel')
      this.ruleForm.pwd = localStorage.getItem('pwd')
    }
  },
  methods: {
    // 表单的验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 判断是否记住密码
          if (this.checked) {
            // 记住密码
            localStorage.setItem('tel', this.ruleForm.tel)
            localStorage.setItem('pwd', this.ruleForm.pwd)
            localStorage.setItem('checked', this.checked)
          } else {
            // 不记住密码
            localStorage.removeItem('tel')
            localStorage.removeItem('pwd')
          }
          // 验证成功
          const pwd = md5(this.ruleForm.pwd, 'hex')
          managerLogin({ ...this.ruleForm, pwd } ).then(res => {
            const { token, userId } = res
            localStorage.setItem('token', token)
            localStorage.setItem('useId', userId)
            this.$router.push('/user')
          }).catch(err => {
            console.log(err);
          })
        } else {
          // 验证失败
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
}
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  background-size: cover !important;
  background: url(../assets/image/bg.jpg) no-repeat;
  position: relative;
}

.login_box {
  width: 300px;
  background-color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  padding: 40px 40px 20px 40px;
}

.login-title {
  text-align: center;
  font-size: 30px;
  color: #333;
  margin-bottom: 30px;
}
</style>